package day15;

import java.util.Comparator;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

//需求：往TreeSet中存入字符串，让字符串按照长度排序
public class TreeSetStringDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Set<String> set = new TreeSet<String>(new StringComparator());
		set.add("java01");
		set.add("java0101");
		set.add("java0");
		set.add("jav");
		set.add("java");
		set.add("java02");
		set.add("java04");
		for (Iterator<String> iterator = set.iterator(); iterator.hasNext();) {
			String string = iterator.next();
			System.out.println(string);
		}
	}

}
//需求：往TreeSet中存入字符串，让字符串按照长度排序
class StringComparator implements Comparator<String> {

	@Override
	public int compare(String str1, String str2) {
		
		int num = str1.length() - str2.length();
		//int num = new Integer(str1.length()).compareTo(new Integer(str2.length()));
		if (num == 0) {
			return str1.compareTo(str2);
		}
		return num;
	}
	
}



